package com.wc.算法提高课.D第四章_高级数据结构.AC自动机.搜索关键词;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/29 0:04
 * @description https://www.acwing.com/problem/content/1284/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 10010, S = 55, M = 1000010, idx = 1;
    static char[] str;
    static int[][] tr = new int[N * S][26];
    static int[] ne = new int[N * S], q = new int[N * S], cnt = new int[N * S];
    static int n;

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            for (int i = 0; i < tr.length; i++) {
                Arrays.fill(tr[i], 0);
                ne[i] = q[i] = 0;
            }
            n = sc.nextInt();
            for (int i = 0; i < n; i++) {
                str = sc.next().toCharArray();
                insert();
            }
            build();
            str = sc.next().toCharArray();
            int res = 0;
            for (int i = 0, j = 0; i < str.length; i++) {
                int t = str[i] - 'a';
                j = tr[j][t];
//                while (j > 0 && tr[j][t] == 0) j = ne[j];
//                if (tr[j][t] > 0) j = tr[j][t];
                int p = j;
                while (p > 0) {
                    res += cnt[p];
                    cnt[p] = 0;
                    p = ne[p];
                }
            }
            out.println(res);
            out.flush();
        }
    }

    static void insert() {
        int p = 0;
        for (char c : str) {
            int t = c - 'a';
            if (tr[p][t] == 0) tr[p][t] = idx++;
            p = tr[p][t];
        }
        cnt[p]++;
    }

    static void build() {
        int hh = 1, tt = 0;
        for (int i = 0; i < 26; i++) {
            if (tr[0][i] > 0) q[++tt] = tr[0][i];
        }
        while (hh <= tt) {
            int t = q[hh++];
            for (int i = 0; i < 26; i++) {
                int p = tr[t][i];
                if (p == 0) tr[t][i] = tr[ne[t]][i];
                else {
                    ne[p] = tr[ne[t]][i];
                    q[++tt] = p;
                }
//                if (p == 0) continue;
//                int j = ne[t];
//                while (j > 0 && tr[j][i] == 0) j = ne[j];
//                if (tr[j][i] > 0) j = tr[j][i];
//                ne[p] = j;
//                q[++tt] = p;
            }
        }
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}